package com.sxz.sbs.widget;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;


/**
 * 打电话
 */

public class PhoneDialog {
    public static void callPhone(final Context context, String phone) {
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShort("无手机号");
           return;
        }
        final String finalPhone = phone;
        new StallAlertDialog.Builder(context)
                .showTitle(false)
                .setMessage(phone)
                .setPositiveButton("呼叫", new StallAlertDialog.OnPositiveListener() {
                    @Override
                    public void onPositiveClick(View view) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            int checkCallPhonePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
                            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE},100);
                                return;
                            } else {
                                //已有权限
                                call( context, finalPhone);
                            }
                        } else {
                            //API 版本在23以下
                            call( context, finalPhone);
                        }
                    }
                }).create().show();
    }
    private static void call(Context context, String phone) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + phone));
        context.startActivity(intent);
    }
}
